package cn.com.dhc.danlu.fragment.enveloplogic;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.base.WebViewActivity;
import cn.com.dhc.danlu.constant.UrlConst;
import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.UrlUtil;
import cn.com.dhc.danlu.widget.adapter.EnvelopStatusAdapter;
import cn.com.dhc.danlu.widget.adapter.MyBaseAdapter;
import cn.com.dhc.danlu.widget.base.BaseListViewLogic;
import cn.com.dhc.danlu.widget.loadlayer.LoaddingLayout;
import cn.com.dhc.mibd.eufw.widget.android.ptr.CustomPullToRefreshListView;
import cn.com.hd.mall.web.webservices.entity.request.envelop.GetMyCouponListRequest;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetMyCouponListResponse;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

public class EnvelopStatusPresenter extends BaseListViewLogic<GetMyCouponListRequest, GetMyCouponListResponse> {
    private int mStatus = 0;

    public EnvelopStatusPresenter(Context context, final CustomPullToRefreshListView ptrListView, int status) {
        super(context, ptrListView);
        this.mStatus = status;
    }

    @Override
    public void setLoaddingLayout(LoaddingLayout mLoaddingLayout) {
        super.setLoaddingLayout(mLoaddingLayout);

        if (mStatus == 0) {
            mLoaddingLayout.changeEmptyViewTips(getContext().getString(R.string.empty_avaiable_envelop));
            mLoaddingLayout.setEmptyViewRetryClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 跳转到活动中心界面
                    WebViewActivity.startSelf(getContext(), CommonUtils.getStringByResId(R.string.activity_center), UrlUtil.getActivityUrl(UrlConst.URL_DEFAULT), false);
                }
            });
            mLoaddingLayout.changeEmptyRetryText(getContext().getString(R.string.go_view));
            mLoaddingLayout.setErrorViewRetryClickListener(retryListener);
        } else if (mStatus == 1) {
            mLoaddingLayout.changeEmptyViewTips(getContext().getString(R.string.empty_hasuse_envelop));
            mLoaddingLayout.setEmptyViewRetryClickListener(retryListener);
            mLoaddingLayout.setErrorViewRetryClickListener(retryListener);
        } else {
            mLoaddingLayout.changeEmptyViewTips(getContext().getString(R.string.empty_overdue_envelop));
            mLoaddingLayout.setEmptyViewRetryClickListener(retryListener);
            mLoaddingLayout.setErrorViewRetryClickListener(retryListener);
        }
    }

    @Override
    public void callFetchData() {
        GetMyCouponListRequest request = new GetMyCouponListRequest();
        request.setToken(agent.getToken());
        request.setPage(isPullDownRefresh ? 1 : mPage);
        request.setRows(10);
        request.setCompanyId(agent.getLoginUser().getCompanyId());
        String status = "";
        if (mStatus == 0) {
            // 未使用
            status = "01";
        } else if (mStatus == 1) {
            // 已使用
            status = "02";
        } else if (mStatus == 2) {
            // 已过期
            status = "04";
        }
        request.setCouponStatus(status);
        agent.getApi()
                .getMyEnvelopList(request)
                .compose(OperatorUtils.<GetMyCouponListResponse> defaultSchedulers())
                .subscribe(new ListObserver());
    }

    @Override
    protected List getList(GetMyCouponListResponse model) {
        return model.getCouponList();
    }

    @Override
    protected void modifyEmptyReloadShowText() {
        mLoaddingLayout.changeEmptyViewTips(getContext().getString(R.string.empty_avaiable_envelop));
    }

    @Override
    protected void modifyErrorReloadText() {
        mLoaddingLayout.changeEmptyViewTips(getContext().getString(R.string.reload));
    }

    @Override
    protected MyBaseAdapter initAdapter() {
        return new EnvelopStatusAdapter(getContext());
    }
}